package com.rx.mgt.controller;

import com.rx.mgt.domain.AdminUser;
import com.rx.mgt.domain.DomainSign;
import com.rx.mgt.po.UsersBo;
import com.rx.mgt.service.DomainService;
import com.rx.mgt.service.SysLogService;
import com.rx.mgt.util.IpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zxj on 2017/5/31.
 */
@Controller
public class DomainSignController {
    @Autowired
    SysLogService sysLogService;

    @Autowired
    private DomainService domainService;

    @RequestMapping("/domain-sign-add.htmlx")
    public String domianSignInitPage(HttpServletRequest request){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        UsersBo usersBo = new UsersBo();
        List list = domainService.getDomainInfoExt(usersBo);
        String type = user.getType();
        if("2".equals(type)){
            request.setAttribute("dom",user.getDomain());
        }
        request.setAttribute("domainList",list);
        return "domian-sign";
    }

    /**
     * ajax获取签名内容
     */
    @RequestMapping("/ajaxGetDomianSignContent.htmlx")
    @ResponseBody
    public Map<String,Object> ajaxGetDomianSignContent(@RequestParam("domain")String domain){
        Map<String,Object> rs = new HashMap<String,Object>();
        DomainSign domainSign = domainService.getDomainSignContentByDomain(domain);
        rs.put("code","001");
        rs.put("obj",domainSign);
        return rs;
    }

    /**
     * 新增或修改域签名
     *
     */
    @RequestMapping("/saveOrUpdateDomainSign.htmlx")
    @ResponseBody
    public Map<String,Object> saveOrUpdateDomainSign(DomainSign sign,HttpServletRequest request){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        Map<String,Object> rs = new HashMap<String, Object>();
        domainService.saveOrUpdateDomainSing(sign);
        sysLogService.add("新增或修改域签名", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        rs.put("code","001");
        return rs;
    }


}
